package com.eternal.widget.guqiang;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eternal.widget.R;

/**
 * Created by Administrator
 * On 2020/4/29
 * Description:
 * @author Administrator
 */
public class ProgressToolbar extends ConstraintLayout {
    private ImageView back, next;
    private TextView title,subtitle;
    private TextView connectText;
    private View progress;
    private float percent, target, a;
    private ValueAnimator animator;
    private long last, nextTime;

    public ProgressToolbar(Context context) {
        this(context, null);
    }

    public ProgressToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view= View.inflate(context, R.layout.progress_toolbar_layout, this);
        back=view.findViewById(R.id.img_back);
        next=view.findViewById(R.id.img_next);
        title=view.findViewById(R.id.txt_title);
        subtitle=view.findViewById(R.id.txt_subtitle);
        connectText=view.findViewById(R.id.txt_connect_time);
        progress=view.findViewById(R.id.v_progress);
        animator=ValueAnimator.ofInt(0);
        animator.setDuration(1000);
        animator.setRepeatCount(-1);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                long current=System.currentTimeMillis();
                if(current<= nextTime){
                    setProgress(a*(current-last)/(nextTime -last)+(target-a));
                    if(percent==100){
                        animator.end();
                        setProgress(0);
                    }
                }else if(percent!=target){
                    setProgress(target);
                }
            }
        });

        if(attrs!=null){
            TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.ProgressToolbar);
            back.setImageDrawable(array.getDrawable(R.styleable.ProgressToolbar_backRes));
            next.setImageDrawable(array.getDrawable(R.styleable.ProgressToolbar_nextRes));
            title.setText(array.getText(R.styleable.ProgressToolbar_title));
            subtitle.setText(array.getText(R.styleable.ProgressToolbar_subtitle));
            array.recycle();
        }
    }

    public void setTitle(String title){
        this.title.setText(title);
    }

    public void setSubtitle(String subtitle) {
        this.subtitle.setText(subtitle);
    }

    public ImageView getBack(){
        return back;
    }

    public ImageView getNext(){
        return next;
    }

    public void setTime(String time){
        connectText.setText(time);
    }

    public void setTimeColor(int color){
        connectText.setTextColor(color);
    }

    public void setAnimatorProgress(float percent){
        if(this.percent>percent){
            return;
        }
        a=(percent-target)*1.5f;
        target=target+a;
        long current=System.currentTimeMillis();
        nextTime = last+(long) ((current-last)*1.5f);
        last=current;
    }

    public void startProgress(){
        if(!animator.isStarted()){
            target=0;
            percent=0;
            animator.start();
            last=System.currentTimeMillis();
        }
    }

    public void initProgress(float distance, int duration){
        if(target==0){
            a=distance;
            target=distance;
            nextTime=last+duration;
            last=System.currentTimeMillis();
        }
    }

    public void endProgress(final Callback callback){
        if(animator.isStarted()){
            animator.end();
            if(percent<100){
                ValueAnimator temp=ValueAnimator.ofFloat(percent, 100);
                temp.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        setProgress(((float) animation.getAnimatedValue()));
                    }
                });
                temp.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        setProgress(0);
                        if(callback!=null){
                            callback.onEnd();
                        }
                    }
                });
                temp.setDuration(500);
                temp.start();
            }else{
                setProgress(0);
                if(callback!=null){
                    callback.onEnd();
                }
            }
        }
    }

    public void setProgress(float percent){
        if(percent!=this.percent){
            ViewGroup.LayoutParams params =progress.getLayoutParams();
            params.width= (int) (getWidth()*percent/100f);
            progress.setLayoutParams(params);
            this.percent=percent;
        }
    }

    public void cancelProgress(){
        if(animator.isStarted()){
            animator.end();
        }
        setProgress(0);
    }

    public interface Callback{
        void onEnd();
    }
}